package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.toyhouse.ToyHouseBlock;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseBlockData;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseOrder;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseOrderProg;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseUserData;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseBlockConfig;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseCombConfig;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseConfig;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseOrderConfig;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseScoreConfig;
import com.yanqu.road.entity.activity.toyhouse.eliminate.ToyHouseEliminateBoom;
import com.yanqu.road.entity.activity.toyhouse.eliminate.ToyHouseEliminateRoundResult;
import com.yanqu.road.entity.activity.toyhouse.eliminate.ToyHouseEliminateStageResult;
import com.yanqu.road.pb.activity.ToyHouseProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;

public class ToyHousePb {

    public static ToyHouseProto.ToyHouseEnergyMsg.Builder parseToyHouseEnergyMsg(ToyHouseUserData userData) {
        ToyHouseProto.ToyHouseEnergyMsg.Builder builder = ToyHouseProto.ToyHouseEnergyMsg.newBuilder();
        builder.setEnergy(userData.getEnergy());
        builder.setRecoveryTime(userData.getRecoveryTime());
        return builder;
    }

    public static ToyHouseProto.ToyHouseSyncUserDataRespMsg.Builder parseToyHouseSyncUserDataRespMsg(ToyHouseUserData userData) {
        ToyHouseProto.ToyHouseSyncUserDataRespMsg.Builder builder = ToyHouseProto.ToyHouseSyncUserDataRespMsg.newBuilder();
        builder.setEnergyMsg(parseToyHouseEnergyMsg(userData));
        builder.setToyData(PropertyHelper.parsePropertyToString(userData.getToyProp()));
        builder.setCandyCount(userData.getCandyCount());
        return builder;
    }

    public static ToyHouseProto.ToyHouseSyncConfigRespMsg.Builder parseToyHouseSyncConfigRespMsg(ToyHouseConfig config, String language) {
        ToyHouseProto.ToyHouseSyncConfigRespMsg.Builder builder = ToyHouseProto.ToyHouseSyncConfigRespMsg.newBuilder();
        for (ToyHouseOrderConfig orderConfig : config.getOrderConfigMap().values()) {
            builder.addOrderConfig(parseToyHouseOrderConfigMsg(orderConfig, language));
        }
        for (ToyHouseBlockConfig blockConfig : config.getBlockConfigMap().values()) {
            builder.addBlockConfig(parseToyHouseBlockConfigMsg(blockConfig, language));
        }
        for (ToyHouseScoreConfig scoreConfig : config.getScoreConfigMap().values()) {
            builder.addScoreConfig(parseToyHouseScoreConfigMsg(scoreConfig));
        }
        for (ToyHouseCombConfig combConfig : config.getCombConfigMap().values()) {
            builder.addCombConfig(parseToyHouseCombConfigMsg(combConfig, language));
        }
        return builder;
    }

    public static ToyHouseProto.ToyHouseCombConfigMsg.Builder parseToyHouseCombConfigMsg(ToyHouseCombConfig config, String language) {
        ToyHouseProto.ToyHouseCombConfigMsg.Builder builder = ToyHouseProto.ToyHouseCombConfigMsg.newBuilder();
        builder.setCombId(config.getCombId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setElement(config.getElement());
        return builder;
    }

    public static ToyHouseProto.ToyHouseBlockConfigMsg.Builder parseToyHouseBlockConfigMsg(ToyHouseBlockConfig config, String language) {
        ToyHouseProto.ToyHouseBlockConfigMsg.Builder builder = ToyHouseProto.ToyHouseBlockConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setType(config.getType());
        return builder;
    }

    public static ToyHouseProto.ToyHouseOrderConfigMsg.Builder parseToyHouseOrderConfigMsg(ToyHouseOrderConfig config, String language) {
        ToyHouseProto.ToyHouseOrderConfigMsg.Builder builder = ToyHouseProto.ToyHouseOrderConfigMsg.newBuilder();
        builder.setOrderId(config.getOrderId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setItemDesc(ServerLanguageMgr.getContent(config.getItemDesc(), language));
        builder.setMulti(config.getMulti());
        builder.setBlockList(config.getBlockList());
        builder.setScore(config.getScore());
        builder.setSpecParam(config.getSpecParam());
        builder.setWeight(config.getWeight());
        builder.setDesc(config.getDesc());
        return builder;
    }

    public static ToyHouseProto.ToyHouseScoreConfigMsg.Builder parseToyHouseScoreConfigMsg(ToyHouseScoreConfig config) {
        ToyHouseProto.ToyHouseScoreConfigMsg.Builder builder = ToyHouseProto.ToyHouseScoreConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setContinuousNum(config.getContinuousNum());
        builder.setPic(config.getPic());
        builder.setScoreAdd(config.getScoreAdd());
        return builder;
    }

    public static ToyHouseProto.ToyHouseGameDataMsg.Builder parseToyHouseGameDataMsg(ToyHouseUserData userData, int multi) {
        ToyHouseProto.ToyHouseGameDataMsg.Builder builder = ToyHouseProto.ToyHouseGameDataMsg.newBuilder();
        builder.addAllBlockMsg(parseToyHouseBlockMsgList(userData.getBlockDataMap().get(multi)));
        builder.setOrderMsg(parseToyHouseOrderMsg(userData.getOrderMap().get(multi)));
        builder.setEnergyMsg(parseToyHouseEnergyMsg(userData));
        builder.setScore(userData.getScore());
        builder.setMulti(multi);
        builder.setToyData(PropertyHelper.parsePropertyToString(userData.getToyProp()));
        builder.setCandyCount(userData.getCandyCount());
        return builder;
    }

    public static List<ToyHouseProto.ToyHouseBlockMsg> parseToyHouseBlockMsgList(ToyHouseBlockData blockData) {
        List<ToyHouseProto.ToyHouseBlockMsg> msgList = new ArrayList<>();
        int[][] blockList = blockData.getBlockList();
        for (int y = 0; y < blockList.length; y++) {
            for (int x = 0; x < blockList[y].length; x++) {
                ToyHouseProto.ToyHouseBlockMsg.Builder builder = ToyHouseProto.ToyHouseBlockMsg.newBuilder();
                builder.setX(x);
                builder.setY(y);
                builder.setColor(blockList[y][x]);
                msgList.add(builder.build());
            }
        }
        return msgList;
    }

    public static ToyHouseProto.ToyHouseOrderMsg.Builder parseToyHouseOrderMsg(ToyHouseOrder order) {
        ToyHouseProto.ToyHouseOrderMsg.Builder builder = ToyHouseProto.ToyHouseOrderMsg.newBuilder();
        builder.setOrderId(order.getoId());
        for (ToyHouseOrderProg prog : order.getProgMap().values()) {
            builder.addProgList(parseToyHouseOrderProgMsg(prog));
        }
        return builder;
    }

    public static ToyHouseProto.ToyHouseOrderProgMsg.Builder parseToyHouseOrderProgMsg(ToyHouseOrderProg prog) {
        ToyHouseProto.ToyHouseOrderProgMsg.Builder builder = ToyHouseProto.ToyHouseOrderProgMsg.newBuilder();
        builder.setBlockId(prog.getbId());
        builder.setCur(prog.getCur());
        builder.setNeed(prog.getNeed());
        return builder;
    }

    public static ToyHouseProto.ToyHouseEliminateRoundMsg.Builder parseToyHouseEliminateRoundMsg(ToyHouseEliminateRoundResult result) {
        ToyHouseProto.ToyHouseEliminateRoundMsg.Builder builder = ToyHouseProto.ToyHouseEliminateRoundMsg.newBuilder();
        builder.setType(result.getType());
        for (ToyHouseEliminateStageResult stageResult : result.getStageResultList()) {
            builder.addStageList(parseToyHouseEliminateStageMsg(stageResult));
        }
        for (ToyHouseBlock block : result.getCreateBomb()) {
            builder.addCreateBombList(parseToyHouseBlockMsg(block));
        }
        if (result.getOrderBomb() != null) {
            builder.setOrderGiveBombList(parseToyHouseBlockMsg(result.getOrderBomb()));
        }
        for (int y = 0; y < result.getBlockList().length; y++) {
            for (int x = 0; x < result.getBlockList()[y].length; x++) {
                builder.addBlockMsgList(parseToyHouseBlockMsg(x, y, result.getBlockList()[y][x]));
            }
        }
        builder.setCurOrder(parseToyHouseOrderMsg(result.getCurOrder()));
        if (result.getFinishOrder() != null) {
            builder.setFinishOrder(parseToyHouseOrderMsg(result.getFinishOrder()));
        }
        if (result.getFailOrder() != null) {
            builder.setFailOrder(parseToyHouseOrderMsg(result.getFailOrder()));
        }
        return builder;
    }

    public static ToyHouseProto.ToyHouseEliminateStageMsg.Builder parseToyHouseEliminateStageMsg(ToyHouseEliminateStageResult stageResult) {
        ToyHouseProto.ToyHouseEliminateStageMsg.Builder builder = ToyHouseProto.ToyHouseEliminateStageMsg.newBuilder();
        for (ToyHouseEliminateBoom eliminateBoom : stageResult.getBoomList()) {
            builder.addBoomList(parseToyHouseBoomMsg(eliminateBoom));
        }
        for (ToyHouseBlock drinkBlock : stageResult.getBlockList()) {
            builder.addBlockList(parseToyHouseBlockMsg(drinkBlock));
        }
        return builder;
    }

    public static ToyHouseProto.ToyHouseBoomMsg.Builder parseToyHouseBoomMsg(ToyHouseEliminateBoom boom) {
        ToyHouseProto.ToyHouseBoomMsg.Builder builder = ToyHouseProto.ToyHouseBoomMsg.newBuilder();
        builder.setX(boom.getX());
        builder.setY(boom.getY());
        builder.setType(boom.getType());
        return builder;
    }

    public static ToyHouseProto.ToyHouseBlockMsg.Builder parseToyHouseBlockMsg(ToyHouseBlock drinkBlock) {
        ToyHouseProto.ToyHouseBlockMsg.Builder builder = ToyHouseProto.ToyHouseBlockMsg.newBuilder();
        builder.setX(drinkBlock.getX());
        builder.setY(drinkBlock.getY());
        builder.setColor(drinkBlock.getC());
        builder.setScore(drinkBlock.getScore());
        return builder;
    }

    /**
     * 方块（不带积分）
     */
    public static ToyHouseProto.ToyHouseBlockMsg.Builder parseToyHouseBlockMsg(int x, int y, int c) {
        ToyHouseProto.ToyHouseBlockMsg.Builder builder = ToyHouseProto.ToyHouseBlockMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        builder.setColor(c);
        builder.setScore(0);
        return builder;
    }

}
